package 反射;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author pppppp
 * @date 2022/4/28 10:02
 *
 */
public class _1_访问字段 {
    public static void main(String[] args) throws Exception {
        Class stdClass = Student.class;
        // 获取public字段"score":
        // System.out.println(stdClass.getField("score"));
        // // 获取继承的public字段"name":
        // System.out.println(stdClass.getField("name"));
        // // 获取private字段"grade":
        // System.out.println(stdClass.getDeclaredField("grade"));

        /*for (Field field : stdClass.getFields()) {
            System.out.println(field);
        }
        System.out.println();
        for (Field declaredField : stdClass.getDeclaredFields()) {
            System.out.println(declaredField);
        }*/

        // Field grade = Student.class.getDeclaredField("grade");
        // grade.setAccessible(true);
        // System.out.println(grade);
       /* Student student = new Student();
        Method getScore = Student.class.getDeclaredMethod("getScore");
        getScore.setAccessible(true);
        Object invoke = getScore.invoke(student);
        System.out.println(invoke);*/

        /*获取父类的private字段*/
        Field parentName = Student.class.getSuperclass().getDeclaredField("parentName");
        System.out.println(parentName);
    }
}

class Student extends Person {
    public int score;
    private int grade;
    private int getScore(){
        return score;
    }
}

class Person extends Human{
    public String name;
    private String parentName;
}

class Human {
    public String humanName;
}
